package cn.imnaer.service.rpc.common;

import java.util.ArrayList;
import java.util.List;

import org.bson.BSONObject;

public class CommonRpcApiImpl {
    private static final long DEFAULT_PAGE_SIZE = 20;
	protected <T> T bsonObejctToBean(BSONObject bsonObject, Class<T> cls) {
        T vo = null;
        try {
            if (bsonObject != null) {
                vo = bsonObject.as(cls);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return vo;
    }
    protected <T> List<T> bsonObejctsToBeanList(List<BSONObject> bsonObjectList, Class<T> cls) {
        T vo = null;
        List<T>  beanList=new ArrayList<>();
        if(bsonObjectList==null ||bsonObjectList.size()==0){      	
            return  beanList;	
        }
        try {
        	for (BSONObject bsonObject : bsonObjectList) {
                     vo = bsonObject.as(cls);
                     beanList.add(vo);
			}         
        } catch (Exception e) {
            e.printStackTrace();
            return beanList;
        }
        return beanList;
    }
    public <T> int getPageCount(List<T> result) {
		long count = result.size();
		if (count % DEFAULT_PAGE_SIZE == 0) {
			return (int) (count / DEFAULT_PAGE_SIZE);
		} else {
			return (int) ((count / DEFAULT_PAGE_SIZE) + 1);
		}
	}
}
